﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace E1_eMart
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public TaiKhoan gTaiKhoan = new TaiKhoan();
        protected int loaiTaiKhoan = -1;
        protected List<SanPham> splist = new List<SanPham>();
        protected List<LoaiSanPham> lsplist = new List<LoaiSanPham>();
        protected List<SanPham> topsplist = new List<SanPham>();
	    protected string catID = "1";
        protected int page = 1;
        protected int totalpage = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            catID = Request["catID"];

	        if(Request["page"] != null)
                page = int.Parse(Request["page"]);

            DataClasses1DataContext edata = new DataClasses1DataContext();

            var itemQuery3 = from sp in edata.LoaiSanPhams
                             select sp;
            lsplist = itemQuery3.ToList();            

            List<int> idtemplist = new List<int>();
            var itemQuery4 = from ct in edata.ChiTietDauGias
                             group ct by ct.MaSanPham into gr
                             orderby gr.Count() descending
                             select new { id = gr.Key.ToString() };

            foreach (var idtemp in itemQuery4)
            {
                idtemplist.Add(int.Parse(idtemp.id));
            }

            var itemQuery5 = from sp in edata.SanPhams
                             where idtemplist.Contains(sp.MaSanPham)
                             select sp;

            topsplist = itemQuery5.Take(8).ToList();

            if (String.IsNullOrEmpty(Request["catID"]))
            {
                ((DropDownList)this.Master.FindControl("catSelector")).SelectedIndex = 0;

                var itemQuery = from sp in edata.SanPhams
                                orderby sp.MaSanPham descending
                                select sp;
                splist = itemQuery.ToList();

                totalpage = (splist.Count - 1) / 8 + 1;
                splist = itemQuery.Skip(8 * (page - 1)).Take(8).ToList();
                
            }

            else
            {
                ((DropDownList)this.Master.FindControl("catSelector")).SelectedIndex = int.Parse(catID);

                var itemQuery2 = from sp in edata.SanPhams
                                 where sp.MaLoaiSanPham == int.Parse(catID)
                                 select sp;
                splist = itemQuery2.ToList();

                totalpage = (splist.Count - 1) / 8 + 1;
                splist = itemQuery2.Skip(8 * (page - 1)).Take(8).ToList();
            }
            if (Session["username"] != null)
            {
                gTaiKhoan = (TaiKhoan)Session["username"];
                loaiTaiKhoan = (int)gTaiKhoan.MaLoaiTaiKhoan;
            }
        }
    }
}